/*
 *    This file is part of BlobDB.
 *
 *    BlobDB is free software: you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation, either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    BlobDB is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with BlobDB.  If not, see <http://www.gnu.org/licenses/>.
 *    
 *    Copyright(c) 2011 Michael Kankkonen
 *    http://www.vnetcon.org
 */


package org.vnetcon.blobdb.database.backend.gae;

import org.vnetcon.blobdb.database.backend.KeyInterface;
import org.vnetcon.blobdb.database.backend.TableDataInterface;
import org.vnetcon.blobdb.database.backend.gae.jdo.TableData;

import com.google.appengine.api.datastore.Blob;
import com.google.appengine.api.datastore.Key;

public class GAETableData implements TableDataInterface {

	TableData nativeTable = null;
	
	public GAETableData(TableData nativeTable){
		this.nativeTable = nativeTable;
	}
	
	@Override
	public byte[] getBytes(boolean forWrite) {
		Blob b = this.nativeTable.getDataRows();
		if(b == null){
			return null;
		}
		return b.getBytes();
	}

	@Override
	public int getFreeRows() {
		return this.nativeTable.getFreeRows();
	}

	@Override
	public void setLastRowid(int lastRowid) {
		this.nativeTable.setLastRowid(lastRowid);
	}

	@Override
	public void setDataRows(byte[] rows) {
		Blob b = new Blob(rows);
		this.nativeTable.setDataRows(b);
	}

	@Override
	public int getLastRowid() {
		return this.nativeTable.getLastRowid();
	}

	@Override
	public byte[] getDataRows(boolean forWrite) {
		Blob b = this.nativeTable.getDataRows();
		return b.getBytes();
	}

	@Override
	public int getTableID() {
		return this.nativeTable.getTableID();
	}

	@Override
	public int getTableDataID() {
		return this.nativeTable.getTableDataID();
	}

	@Override
	public void setRowCount(int rowcount) {
		this.nativeTable.setRowCount(rowcount);
	}

	@Override
	public void setFreeRows(int freerows) {
		this.nativeTable.setFreeRows(freerows);
	}

	@Override
	public int getRowCount() {
		return this.nativeTable.getRowCount();
	}

	@Override
	public void touch(long l) {
		this.nativeTable.touch(l);
	}

	@Override
	public TableDataInterface newInstance(int newTableDataID, int updateTableID) {
		TableData ta = new TableData(newTableDataID, updateTableID);
		GAETableData gaeta = new GAETableData(ta);
		return gaeta;
	}

	@Override
	public KeyInterface getKey() {
		GAEKey retKey = new GAEKey();
		retKey.setKeyValue(this.nativeTable.getTableDataKey());
		return retKey;
	}

	@Override
	public void setTableKey(KeyInterface key) {
		Key nativeKey = (Key)key.getKeyValue();
		this.nativeTable.setTableDataKey(nativeKey);
	}

	@Override
	public String getNameForKey() {
		return TableData.class.getSimpleName();
	}

	@Override
	public Object getNativeTable() {
		return this.nativeTable;
	}


}
